Bug fix for Dictionary and Mapping type hints inside of a Pydantic model#1631
Closed
claythearc wants to merge 6 commits intofastapi:mainfrom
Closed
Bug fix for Dictionary and Mapping type hints inside of a Pydantic model#1631claythearc wants to merge 6 commits intofastapi:mainfrom
claythearc wants to merge 6 commits intofastapi:mainfrom
Conversation
Fixed a bug where using Dict or Mapping type hints with Relationship() would cause infinite recursion. The get_relationship_to() function now properly handles dict/Mapping types by extracting the value type (second type argument), similar to how it handles List types. Changes: - Added handling for dict and Mapping origins in get_relationship_to() - Extracts the value type from Dict[K, V] or Mapping[K, V] annotations - Added comprehensive tests for Dict relationships with attribute_mapped_collection This resolves the RecursionError that occurred when defining relationships like: children: Dict[str, Child] = Relationship(...) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
…cursion-011CUbiwV1LogX5vM5VcjpbW Fix RecursionError with Dict relationships in get_relationship_to()
Member
|
We already have #1287 that fixes this issue. |
YuriiMotov
reviewed
Oct 29, 2025
| class Parent(SQLModel, table=True): | ||
| __tablename__ = "parent" | ||
| id: int = Field(primary_key=True) | ||
| children: Optional[Dict[str, "Child"]] = Relationship( |
Member
There was a problem hiding this comment.
Does it make sense to use Optional here?
Author
There was a problem hiding this comment.
I think you can make a reasonable argument either way. After going back and forth with Claude some, I decided that it made sense because it lets you distinguish between not loaded and empty, but I dont have exceptionally strong feelings and can change it.
When using Annotated[type, Field(sa_column=...), Validator(...)], Pydantic V2 creates a new pydantic.fields.FieldInfo that doesn't preserve SQLModel-specific attributes like sa_column and sa_type. This caused timezone-aware datetime columns defined with DateTime(timezone=True) to lose their timezone setting. The fix extracts the SQLModel FieldInfo from the original Annotated type's metadata, preserving sa_column and sa_type attributes even when Pydantic V2 merges the field info. Fixes timezone-aware datetime columns losing timezone=True when using Annotated types with Pydantic validators.
…-01QW9NWe9vXN7RPhTDLfCs6m Fix sa_column/sa_type lost when using Annotated with validators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relates to: #835
Quick little fix for a recursion bug when annotating a member as a Dictionary or Mapping.